<?php
namespace app\common\model;
use x\Xbase;
class GoodsPrice extends Xbase
{
    /**
     * 获取属性数组
     * @param [type] $attr [description]
     */
    public function CountAttr($attr){
        asort($attr);
        foreach($attr as $k => $v){
            $attr[$k] = array_unique(array_filter(explode(',',$v)));
        }
        $rs = ['spec'=>$attr];
        sort($attr);
        $rs['arr'] = $this->toArr($attr);
        return $rs;
    }


    /**
     * 递归将属性变为数组
     * @param  [type] $attr [description]
     * @return [type]       [description]
     */
    public function toArr($attr){
        if(count($attr)<2){
            return isset($attr[0]) ? $attr[0] : [];
        }else{
            $one = $attr[0];
            $two = $attr[1];
            $temp = [];
            foreach($one as $v){
                foreach($two as $val){
                    $temp[] = $v.'_'.$val;
                }
            }
            //重新组成数组
            unset($attr[0]);
            unset($attr[1]);
            $new = [$temp];
            foreach($attr as $v){
                $new[] = $v;
            }
            return $this->toArr($new);
        }
    }

}
